""" _*_ coding : utf-8 _*_
 @Time: 2025/7/25 19:36
 @Author : 虎
 @File : models
 @Project : fastApiProject
"""
from datetime import datetime

from tortoise.models import Model
from tortoise import fields
import uuid

"""
gitee:PyCharm 中 git 先commit再push
PyCharm 中 git cvs Stash Changes 再 pull 拉取
"""


class Role(Model):
    id = fields.IntField(pk=True)
    name = fields.CharField(max_length=255, unique=True)  # 添加unique确保角色名唯一

    class Meta:
        table = "user_roles"


class User(Model):
    id = fields.IntField(pk=True)
    username = fields.CharField(max_length=255, unique=True)  # 用户名唯一
    password = fields.CharField(max_length=255)
    #邮箱
    # email = fields.CharField(max_length=255, unique=True)
    # 允许null或设置默认值 # 自动创建 role_id
    role = fields.ForeignKeyField('models.Role', related_name='users', null=True)

    class Meta:
        table = "users"


class Token(Model):
    id = fields.UUIDField(pk=True, default=uuid.uuid4)
    token = fields.CharField(max_length=255, unique=True)
    user = fields.ForeignKeyField('models.User', related_name='tokens')
    created_at = fields.DatetimeField(auto_now_add=True)
    expires_at = fields.DatetimeField()
    is_active = fields.BooleanField(default=True)

    class Meta:
        table = "user_tokens"

# CellType 表（细胞类型）
# class CellType(Model):
#     name = fields.CharField(max_length=255, unique=True, verbose_name="类型名称", default="未知")
#     description = fields.TextField(null=True, blank=True, verbose_name="类型描述", default="无")
#     created_at = fields.DatetimeField(auto_now_add=True, verbose_name="创建时间", default=datetime.now())
#     updated_at = fields.DatetimeField(auto_now=True, verbose_name="更新时间", default=datetime.now())
#
#     def __str__(self):
#         return self.name


# Cell 表（细胞）
#
# class Cell(Model):
#     name = fields.CharField(max_length=255, unique=True, verbose_name="细胞名称")
#     cell_type = fields.ForeignKeyField(
#         'models.CellType',
#         related_name='cells',
#         on_delete=fields.CASCADE,  # 如果CellType被删除，关联的Cell也会被删除
#         verbose_name="细胞类型"
#     )
#     description = fields.TextField(null=True, blank=True, verbose_name="细胞描述", default="无")
#     is_active = fields.BooleanField(default=True, verbose_name="是否活跃")
#     created_at = fields.DatetimeField(auto_now_add=True, verbose_name="创建时间", default=datetime.now())
#     updated_at = fields.DatetimeField(auto_now=True, verbose_name="更新时间", default=datetime.now())
#
#     def __str__(self):
#         return self.name
#
#
# # Simple 表（简单记录）
# class Simple(Model):
#     entry_time = fields.DatetimeField(auto_now_add=True, verbose_name="录入时间")
#     cell_type = fields.ForeignKeyField(
#         'models.CellType',
#         related_name='simples',
#         on_delete=fields.CASCADE,
#         verbose_name="关联细胞类型"
#     )
#     cell = fields.ForeignKeyField(
#         'models.Cell',
#         related_name='simples',
#         on_delete=fields.CASCADE,
#         verbose_name="关联细胞"
#     )
#     value = fields.FloatField(null=True, blank=True, verbose_name="记录值")
#     notes = fields.TextField(null=True, blank=True, verbose_name="备注")
#     is_valid = fields.BooleanField(default=True, verbose_name="是否有效")
#
#     class Meta:
#         unique_together = ('cell_type', 'cell', 'entry_time')  # 防止重复记录
#
#     def __str__(self):
#         return f"{self.entry_time} - {self.cell.name}"
